package player 
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.flixel.FlxBasic;
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.system.input.Keyboard;
	import player.sucker.Vortex;
	import pessoas.*;
	/**
	 * ...
	 * @author João Brant
	 */
	public class PlayerController 
	{
		
		private static var ACELLERATION:FlxPoint = new FlxPoint(80, 60);
		private var mainState:MainState;
		private var playerPawn:Salazar;
		private var keyboard:Keyboard;
		private var vortex:Vortex;
		
		private var lockedMidia:Midia;
		
		public var isSucking:Boolean = false;
		
		public function PlayerController(state:MainState, pawn:Salazar) 
		{
			playerPawn = pawn;
			mainState = state;
			vortex = new Vortex(null, null);
			vortex.visible = false;
			state.add(vortex);
		}
		
		private function vortexCollision():Boolean
		{
			var collidedAfterAll:Boolean = false;
			for each(var sprite:FlxSprite in this.mainState.pessoasGroup.members)
			{
				var pessoa:PessoaSugavel = sprite as PessoaSugavel;
				if (pessoa != null)
				{
					var colidiu:Boolean = false;
					
					var angulo:Number = Math.PI - Math.PI * 2 * vortex.angle / 360;
					
					for each(var p:FlxPoint in Vortex.vortexCollisionPoints)
					{
						var p7:FlxPoint = new FlxPoint(p.x * Math.cos(angulo) + p.y * Math.sin(angulo),
														- p.x * Math.sin(angulo) + p.y * Math.cos(angulo));
						p7.x += playerPawn.x + 8;
						p7.y += playerPawn.y - 8;
						if (pessoa.overlapsPoint(p7, true)) colidiu = true;
						
						//Main.ballTest(p7.x, p7.y);
					}
					
					if (colidiu)
					{
						if (pessoa.money > 0)
						{
							var amount:Number = playerPawn.vortexStrength * mainState.popularity / 100.0;
							pessoa.money -= amount;
							mainState.MoneyAmount += amount;
							playerPawn.aumentarPrazer(amount);
							
							//Se ultrapassou a medida
							if (pessoa.money < 0)
							{
								mainState.MoneyAmount += pessoa.money;
								pessoa.money = 0;
							}
						}
						collidedAfterAll = true;
						pessoa.beginSuck();
					}
					else
					{
						pessoa.endSuck();
					}
				}
			}
			return collidedAfterAll;
		}
		private function notSuckingAnybody():void
		{
			isSucking = false;
			for each(var sprite:FlxSprite in this.mainState.pessoasGroup.members)
			{
				var pessoa:PessoaSugavel = sprite as PessoaSugavel;
				if (pessoa != null)
				{
					pessoa.endSuck();
				}
			}
		}
		
		public function prison():void
		{
			if (mainState.equipouSaidaPrisao && !mainState.usouSaidaPrisao)
			{
				mainState.usouSaidaPrisao = true;
				return;
			}
				
			if (playerPawn.prisonCount > 0)
				return;
				
			playerPawn.prisonCount = 8 - ((mainState.temPropina1 ? 1 : 0) + (mainState.temPropina2 ? 1 : 0) + (mainState.temPropina3 ? 2 : 0));
			vortex.visible = false;
			vortex.active = false;
			
			mainState.stopScreen();
			
			notSuckingAnybody();
		}
		
		public function slow(m:Midia):void
		{
			if (mainState.equipouAcessorImprensa && !mainState.usouAcessorImprensa)
			{
				mainState.usouAcessorImprensa = true;
				return;
			}
			
			if (playerPawn.slowed) return;
			
			lockedMidia = m;
			m.beginStalk();
			playerPawn.slowed = true;
		}
		
		public function update():void
		{
			//Slowed por jornalistas hipocritas
			if (playerPawn.slowed && lockedMidia != null)
			{
				mainState.popularity -= playerPawn.popularityDamage;
				
				//Main.ballTest(lockedMidia.x - FlxG.camera.scroll.x, lockedMidia.y - FlxG.camera.scroll.y);
				//Main.ballTest(playerPawn.x - FlxG.camera.scroll.x, playerPawn.y - FlxG.camera.scroll.y);
				
				var dx:Number = playerPawn.x - lockedMidia.x;
				var dy:Number = playerPawn.y - lockedMidia.y;
				var dist:Number = Math.sqrt(dx * dx + dy * dy);
				
				var realDistanceToFree:Number = playerPawn.distanceToFree;
				//Optei por fazer ou exclusivo aqui...
				if (mainState.temCensura3) realDistanceToFree *= 0.5;
				else if (mainState.temCensura2) realDistanceToFree *= 0.7;
				else if (mainState.temCensura1) realDistanceToFree *= 0.85;
				
				if (dist > realDistanceToFree)
				{
					playerPawn.slowed = false;
					lockedMidia.endStalk();
					lockedMidia = null;
				}
			}
			else
			{
				mainState.popularity += playerPawn.popularityRecover;
			}
			
			//Prison
			if (playerPawn.prisonCount > 0) 
			{
				playerPawn.prisonCount -= Main.delta;
				return;
			}
			else
			{
				mainState.resumeScreen();
			}
			
			//Finally, input
			if (FlxG.keys.A || FlxG.keys.LEFT)
			{
				playerPawn.push(-ACELLERATION.x, 0);
			}
			if (FlxG.keys.D || FlxG.keys.RIGHT)
			{
				playerPawn.push(ACELLERATION.x, 0);
			}
			if (FlxG.keys.W || FlxG.keys.UP)
			{
				playerPawn.push(0, -ACELLERATION.y);
			}
			if (FlxG.keys.S || FlxG.keys.DOWN)
			{
				playerPawn.push(0, ACELLERATION.y);
			}
			
			if (FlxG.mouse.getWorldPosition().x < playerPawn.x)
				playerPawn.setScale(-1, playerPawn.scale.y);
			else
				playerPawn.setScale(1, playerPawn.scale.y);
			
			if (FlxG.mouse.pressed())
			{
				vortex.setTargets(playerPawn, FlxG.mouse.getWorldPosition());
				vortex.updatePosition();
				vortex.visible = true;
				vortex.alpha = 0.1 + 0.9 * (mainState.popularity / 100.0);
				vortex.active = true;
				
				vortex.emitSound();
				
				isSucking = vortexCollision();
			}
			else
			{
				isSucking = false;
				vortex.visible = false;
				vortex.active = false;
				
				notSuckingAnybody();
			}
		}
		
	}
	
	

}